package com.shiwaixiangcun.monitor;

import com.rabbit.spring.data.domain.ext.CompanyAware;
import com.rabbit.spring.data.repository.GenericJpaRepositoryFactoryBean;
import com.shiwaixiangcun.core.utils.SpringUtils;
import com.shiwaixiangcun.monitor.domain.User;
import com.shiwaixiangcun.monitor.security.SecurityAuditorAware;
import com.shiwaixiangcun.monitor.security.SecurityUtils;
import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import javax.persistence.EntityManagerFactory;

@EnableJpaAuditing
@EntityScan(basePackages = {"com.shiwaixiangcun.monitor.domain", "com.shiwaixiangcun.core.domain"})
@SpringBootApplication(scanBasePackages = {"com.shiwaixiangcun.monitor", "com.shiwaixiangcun.core"})
@EnableJpaRepositories(
        basePackages = {"com.shiwaixiangcun.monitor.repository", "com.shiwaixiangcun.core.repository"},
        repositoryFactoryBeanClass = GenericJpaRepositoryFactoryBean.class)
public class MonitorServerApplication {

    @Bean
    public SpringUtils springUtils() {
        return new SpringUtils();
    }

    @Bean
    public AuditorAware securityAuditorAware() {
        return new SecurityAuditorAware();
    }

    @Bean
    public CompanyAware<Long> currentCompanyAware() {
        return () -> {
            User user = SecurityUtils.getCurrentUser();
            return user.getOrganizationId();
        };
    }


    public static void main(String[] args) {
        SpringApplication.run(MonitorServerApplication.class, args);
    }

}
